<!--
 * @Description  :
 * @Autor        : guoxi
 * @Date         : 2022-06-29 14:08:57
 * @LastEditors  : Please set LastEditors
 * @LastEditTime : 2022-09-14 18:11:36
 * @FilePath     : \rpms\src\views\document\scientific\patent\index.vue
-->
<template>
  <div class="list-h">
    <div>
      <el-button
        v-auth="`document-scientific-add`"
        type="primary"
        @click="handleCommand"
        >新增</el-button
      >
    </div>
    <el-form
      :inline="true"
      :model="formInline"
      ref="formRef"
      @submit.prevent
      class="demo-form-inline"
    >
      <el-form-item label="专利名称" prop="patenNameLike">
        <el-input class="width-custom120" v-model="formInline.patenNameLike" />
      </el-form-item>

      <el-form-item label="专利类型" prop="patentType">
        <el-select
          clearable
          class="width-custom120"
          v-model="formInline.patentType"
        >
          <el-option
            v-for="item in model.patentTypes"
            :label="item.dicValue"
            :key="item.dicCode"
            :value="item.dicCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="成果来源" prop="achievementSource">
        <el-select
          clearable
          class="width-custom120"
          v-model="formInline.achievementSource"
        >
          <el-option
            v-for="item in model.achievementSources"
            :label="item.dicValue"
            :key="item.dicCode"
            :value="item.dicCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="项目名称" prop="projectNameLike">
        <el-input
          class="width-custom120"
          v-model="formInline.projectNameLike"
        />
      </el-form-item>

      <el-form-item label="申请人" prop="patentOwnerLike">
        <el-input
          class="width-custom120"
          v-model="formInline.patentOwnerLike"
        />
      </el-form-item>
      <el-form-item label="专利申请日期">
        <el-date-picker
          class="width-custom220"
          v-model="model.createTime"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD"
          @change="handleChangeDate"
        />
      </el-form-item>

      <el-form-item label="法律状态" prop="lawStatus">
        <el-select
          clearable
          class="width-custom120"
          v-model="formInline.lawStatus"
        >
          <el-option
            v-for="item in model.lawStatuss"
            :label="item.dicValue"
            :key="item.dicCode"
            :value="item.dicCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="审批状态" prop="approveStatus">
        <el-select
          clearable
          class="width-custom120"
          v-model="formInline.approveStatus"
        >
          <el-option
            v-for="item in approvalList"
            :label="item.dicValue"
            :key="item.dicCode"
            :value="item.dicCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <el-skeleton style="height: 50vh" :rows="8" :loading="loading" animated>
    <el-table
      :data="patentList"
      border
      style="width: 100%"
      v-loading="loading"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
    >
      <template #empty>
        <el-empty :image-size="150" description="暂无数据"></el-empty>
      </template>
      <el-table-column type="index" label="序号">
        <template v-slot="scope">
          <span>{{
            (pageState.pageNum - 1) * pageState.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="patenName" label="专利名称"> </el-table-column>

      <el-table-column prop="applyNum" label="专利申请号"> </el-table-column>
      <el-table-column prop="applyDate" label="专利申请日期"> </el-table-column>
      <el-table-column prop="patentType" label="专利类型">
        <template #default="{ row }">
          {{
            model.patentTypes.find(el => el.dicCode === row.patentType).dicValue
          }}
        </template>
      </el-table-column>
      <el-table-column prop="patentSource" label="专利来源">
        <template #default="{ row }">
          {{
            model.patentSources.find(el => el.dicCode === row.patentSource)
              .dicValue
          }}
        </template>
      </el-table-column>
      <el-table-column label="发明人">
        <template #default="{ row }">
          {{
            JSON.parse(row.inventor)
              .map(el => el.name)
              .join(",")
          }}
        </template>
      </el-table-column>
      <el-table-column label="申请人或权利人">
        <template #default="{ row }">
          {{
            JSON.parse(row.patentOwner)
              .map(el => el.name)
              .join(",")
          }}
        </template>
      </el-table-column>
      <el-table-column prop="lawStatus" label="法律状态">
        <template #default="{ row }">
          {{
            model.lawStatuss.find(el => el.dicCode === row.lawStatus)?.dicValue
          }}
        </template>
      </el-table-column>

      <el-table-column label="附件">
        <template #default="{ row }">
          <el-link
            type="warning"
            v-for="(item, index) in row.attachment"
            :key="index"
            @click="handlePreview(item)"
            >{{ item.fileName }}</el-link
          >
        </template>
      </el-table-column>

      <el-table-column prop="approveStatus" label="审批状态">
        <template #default="{ row }">
          {{
            approvalList?.length && approvalList.find(el => el.dicCode === row.approveStatus)?.dicValue
          }}
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            v-auth="`document-scientific-look`"
            type="text"
            size="small"
            @click="handleEdit(row, 'look')"
            >查看</el-button
          >
          <template v-if='row.approveStatus == 3'>
            <el-button
              v-auth="`document-scientific-edit`"
              type="text"
              size="small"
              @click="handleEdit(row, 'edit')"
              >编辑</el-button
            >
          </template>

          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除该条记录吗？"
            @confirm="handleDelete(row)"
          >
            <template #reference>
              <el-button v-auth="`document-scientific-del`" type="text"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <re-pagination
      v-if="model.total > 0"
      :total="model.total"
      v-model:page="pageState.pageNum"
      v-model:limit="pageState.pageSize"
      @pagination="getPatentList"
    />
  </el-skeleton>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from "vue";
import { useRouter } from "vue-router";
import { postPatentList, postPatentDelete } from "/@/api/scientific";
import { emitter } from "/@/utils/mitt";
const props = defineProps({
  companyEntityMaxCode: {
    type: String,
    default: ""
  },
  approvalList:{
    type: Array,
    default: ()=>[]
  }
});
const router = useRouter();
const formRef = ref<HTMLFormElement | null>(null) as any;
const loading = ref(false);
let patentList = ref([]);
const formInline = ref({
  companyEntityMaxCode: props.companyEntityMaxCode,
  achievementSource: "",
  patenNameLike: "",
  projectNameLike: "",
  patentType: "",
  patentSource: "",
  patentOwnerLike: "",
  applyDateStrStart: "",
  applyDateStrEnd: "",
  lawStatus: "",
  approveStatus:""
});

watch(
  () => props.companyEntityMaxCode,
  newVal => {
    formInline.value.companyEntityMaxCode = newVal;
  }
);
const pageState = reactive({
  pageNum: 1,
  pageSize: 10
});
const model = reactive({
  total: 0,
  createTime: "",
  patentTypes: [
    { dicCode: "1", dicValue: "发明专利" },
    { dicCode: "2", dicValue: "实用新型专利" },
    { dicCode: "3", dicValue: "外观专利" }
  ],
  lawStatuss: [
    { dicCode: "1", dicValue: "申请中" },
    { dicCode: "2", dicValue: "有效" },
    { dicCode: "3", dicValue: "无效" }
  ],
  achievementSources: [
    { dicCode: "1", dicValue: "项目类" },
    { dicCode: "2", dicValue: "非项目类" }
  ],
  patentSources: [
    { dicCode: "1", dicValue: "首次申请" },
    { dicCode: "2", dicValue: "基于优先权的申请" },
    { dicCode: "3", dicValue: "基于母案的分案申请" },
    { dicCode: "4", dicValue: "外部购买" },
    { dicCode: "5", dicValue: "内部转让" }
  ],
  types: [
    { dicCode: "1", dicValue: "专利" },
    { dicCode: "2", dicValue: "论文" },
    { dicCode: "3", dicValue: "专著" },
    { dicCode: "4", dicValue: "标准" },
    { dicCode: "5", dicValue: "科技奖励" },
    // { dicCode: "6", dicValue: "科技制度建设" },
    { dicCode: "7", dicValue: "软件著作权" },
    { dicCode: "8", dicValue: "其他" }
  ]
});
const handleCommand = () => {
  router.push("/document/scientific/patent?mode=edit");
};
const handleReset = () => {
  formRef.value.resetFields();

  formInline.value.applyDateStrEnd = "";
  model.createTime = "";
};
const handleSearch = () => {
  pageState.pageNum = 1;

  getPatentList();
};

const handleEdit = (item, mode) => {
  router.push({
    path: "/document/scientific/patent",
    query: {
      id: item.id,
      flowId: item.flowId,
      taskId: item.curTaskId,
      instanceId: item.instanceId,
      projectNum: item.projectId,
      isSkipYs: item.isSkipYs,
      rdScientificId:item.rdScientificId,
      nodeId:item.nodeStatusYsCode && item.nodeStatusYsCode.toLocaleUpperCase(),
      mode:mode
    }
  });
};
const handleDelete = async item => {
  const { code } = await postPatentDelete({
    id: item.id
  });
  if (code === 200) {
    getPatentList();
  }
};
const getPatentList = async () => {
  try {
    loading.value = true;
    const { code, data } = await postPatentList({
      ...formInline.value,
      ...pageState
    });
    if (code === 200) {
      patentList.value = data.list;
      patentList.value.map(el => {
        if (el.attachment) {
          el.attachment = JSON.parse(el.attachment);
        }
      });
      model.total = data.rows;
    }
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
  // try {
  //     const { code, data } = await postPatentList({ pageNum: 1, pageSize: 0 });
  //     if (code === 200) {
  //         patentList.value = data.list;
  //     }
  // } catch (err) {
  //     console.log(err);
  // }
};

const handleChangeDate = item => {
  console.log(item, "date");
  if (item) {
    formInline.value.applyDateStrStart = item[0].replace(
      "12:00:00",
      "00:00:00"
    );
    formInline.value.applyDateStrEnd = item[1].replace("12:00:00", "00:00:00");
  } else {
    formInline.value.applyDateStrStart = "";
    formInline.value.applyDateStrEnd = "";
  }
};
const handlePreview = (item: any) => {
  if (item.response) {
    window.open(
      `${location.origin}${location.pathname}#/online?uuidFile=${item?.response.data.uuidFile}&fileName=${item?.response.data.fileName}`
    );
  } else {
    window.open(
      `${location.origin}${location.pathname}#/online?uuidFile=${item.uuidFile}&fileName=${item.fileName}`
    );
  }
};

emitter.on("changeCompany", companyEntityMaxCode => {
  formInline.value.companyEntityMaxCode = companyEntityMaxCode;
  handleSearch();
});
getPatentList();
</script>

<style lang="scss" scoped></style>
